<?php 
class cls_serve_customer extends cls_serve_base
{
	function login($email, $password, $remember_user, $validate_input)
	{
		$customer_data = cls_data_base::get_instance($this->name);
		$md5pwd = md5($password);

		$validate_serve = cls_serve_base::get_instance("validatecode");
		$validate_result = $validate_serve->check_validate_code($validate_input);
		if(!$validate_result['success'])
		{
			return $validate_result;
		}
		
		$result = $customer_data->check_login($email, $md5pwd);
		if ($result['success']) 
		{
			session_start();
			$_SESSION['user'] = $result['ret'];
		}
		if ($remember_user)
		{
			setcookie("b2c_customerid", $result['ret']['ID'], time()+864000);
		}
		else 
		{
			setcookie("b2c_customerid", $result['ret']['ID'], time()+3600);
		}
		return $result;
	}
	
	function register($name, $password, $email, $birthday, $location, $validate_input)
	{
		$validate_serve = cls_serve_base::get_instance("validatecode");
		$validate_result = $validate_serve->check_validate_code($validate_input);
		if(!$validate_result['success'])
		{
			return $validate_result;
		}
	
		$emailcheck = $this->check_email_exist($email);
		if($emailcheck)
		{
			return return_info(false, "邮箱已存在");
		}
		$customer_data = cls_data_base::get_instance($this->name);
		$md5pwd = md5($password);
		$result = $customer_data->register($name, $md5pwd, $email, $birthday, $location);
		if ($result == 0)
		{
			return return_info(false);
		}
		else
		{
			$this->add_email_verify($result, $email);
			return return_info(true, $result);
		}
	}
	
	function check_email_exist($email)
	{
		$customer_data = cls_data_base::get_instance($this->name);
		return $customer_data->check_email_exist($email);
	}
	
	function get_customer_information($customerid)
	{
		$customer_data = cls_data_base::get_instance($this->name);
		$customer_information = $customer_data->get_customer_information($customerid);
		if(!$customer_information)
		{
			return return_info(false);
		}
		else
		{
			return return_info(true, $customer_information);
		}
	}
	
	function get_customer_information_by_email($email)
	{
		$customer_data = cls_data_base::get_instance($this->name);
		$customer_information = $customer_data->get_customer_information_by_email($email);
		if(!$customer_information)
		{
			return return_info(false);
		}
		else
		{
			return return_info(true, $customer_information);
		}
	}

	function update_customer_information($customerid, $name, $birthday, $location)
	{
		$customer_data = cls_data_base::get_instance($this->name);
		
		$result = $customer_data->update_customer_information($customerid, $name, $birthday, $location);

		return return_info(true);
	}
	
	function update_customer_password($customerid, $old_md5pwd, $new_md5pwd)
	{
		$customer_data = cls_data_base::get_instance($this->name);
		
		$result = $customer_data->update_customer_password($customerid, $old_md5pwd, $new_md5pwd);
		return $result;
	}
	
	function reset_password($customerid, $new_pwd, $verifycode)
	{
		$customer_data = cls_data_base::get_instance($this->name);
		$reset_right = $this->check_reset_right($customerid, $verifycode);
		if($reset_right['success']==false)
		{
			return return_info(false, "重置验证码错误");
		}

		// $random_password = $this->random_string(10);
		$new_md5pwd = md5($new_pwd);
		$result = $customer_data->reset_password($customerid, $new_md5pwd);
		
		if($result['success'] == true)
		{
			$this->delete_verify_info($customerid, $verifycode);
		}
		// $sm = new smail("b2c_lh_auto@163.com", "a123456", "smtp.163.com");
		// $end = $sm->send($email, "b2c_lh_auto@163.com", "密码重置", "用户您好\n您邮箱对应的密码已经重置为{$random_password}，请您使用新密码登录网站并及时重置密码。\n\n本邮件由系统自动发出，请勿回复");
		return $result;
	}
	
	function add_email_verify($customerid, $email)
	{
		$customer_data = cls_data_base::get_instance($this->name);
		
		$verify_code = $this->random_string(10);
		$result = $customer_data->add_email_verify($customerid, $verify_code);
		
		$host = cls_config_index::$host;
		$link = $host . "index.php?act=customer.active&customerid=" . $customerid . "&verifycode=" . $verify_code;
		
		$sm = new smail("b2c_lh_auto@163.com", "a123456", "smtp.163.com");
		$end = $sm->send($email, "b2c_lh_auto@163.com", "账号激活", "用户您好\n您的邮箱已经在一人一本网站注册，请您点击以下链接来激活您的账号\n".$link."\n\n本邮件由系统自动发出，请勿回复");
	}
	
	function delete_verify_info($customerid, $verify_code)
	{
		$customer_data = cls_data_base::get_instance($this->name);

		$customer_data->delete_verify_info($customerid, $verify_code);
	}

	function add_pwd_reset_verify($email)
	{
		$customer_data = cls_data_base::get_instance($this->name);

		$verify_code = $this->random_string(10);
		$customer_info = $this->get_customer_information_by_email($email);
		if($customer_info == flase)
		{
			return return_info(false, "邮箱不存在");
		}
		if($customer_info['ret']['ID'] == "")
		{
			return return_info(false, "邮箱不存在");
		}
		else
		{
			$customerid = $customer_info['ret']['ID'];
		}
		$result = $customer_data->add_pwd_reset_verify($customerid, $verify_code);

		$host = cls_config_index::$host;
		$link = $host . "resetpassword.php?customerid=" . $customerid . "&verifycode=" . $verify_code;

		$sm = new smail("b2c_lh_auto@163.com", "a123456", "smtp.163.com");
		$end = $sm->send($email, "b2c_lh_auto@163.com", "密码重置", "用户您好\n您的邮箱已经申请重置密码，请您点击以下链接来重置您的密码\n".$link."\n\n本邮件由系统自动发出，请勿回复");

		return return_info(true);
	}

	function verify_email($customerid, $verify_code)
	{
		$customer_data = cls_data_base::get_instance($this->name);
		
		$result = $customer_data->verify_email($customerid, $verify_code);
		if($result['success'] == true)
		{
			$this->delete_verify_info($customerid, $verify_code);
		}
		return $result;
	}
	
	function check_reset_right($customerid, $verify_code)
	{
		$customer_data = cls_data_base::get_instance($this->name);

		$result = $customer_data->check_reset_right($customerid, $verify_code);
		return $result;
	}

	function random_string($length, $max=FALSE)
	{
	  if (is_int($max) && $max > $length)
	  {
		$length = mt_rand($length, $max);
	  }
	  $output = '';
	  
	  for ($i=0; $i<$length; $i++)
	  {
		$which = mt_rand(0,2);
		
		if ($which === 0)
		{
		  $output .= mt_rand(0,9);
		}
		elseif ($which === 1)
		{
		  $output .= chr(mt_rand(65,90));
		}
		else
		{
		  $output .= chr(mt_rand(97,122));
		}
	  }
	  return $output;
	}
}
?>